package com.hk.springbootmybatisplus;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hk.springbootmybatisplus.entity.Employee;
import com.hk.springbootmybatisplus.mapper.EmployeeMapper;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author by 何坤
 * @Classname testMyBatisPlus01
 * @Description TODO
 * @Date 2019/9/28 14:24
 */
@SpringBootTest
@RunWith(SpringRunner.class)
public class testMyBatisPlus01 {
    @Resource
    private EmployeeMapper employeeMapper;

    private Logger logger = LoggerFactory.getLogger(getClass());

    /**
     * 测试mybatis plus基本的insert
     * @param
     * @return void
     * @date 2019/9/28 14:26
     * @author 何坤
     */
    @Test
    public void testInsert01(){
        Employee employee = new Employee();
        employee.setLastName("zhangsan");
        employee.setAge(12);
        employee.setEmail("2946055397@qq.com");
        employee.setGender(0);

        int insert = employeeMapper.insert(employee);
        logger.info(String.valueOf(insert));
        logger.info("插入值的主键值-->"+employee.getId());
    }

    /**
     * 测试删除
     * @param
     * @return void
     * @date 2019/9/28 15:30
     * @author 何坤
     */
    @Test
    public void testDelete(){
        int i = employeeMapper.deleteById(3);
        logger.info(String.valueOf(i));
    }

    /**
     * 测试更新
     * @param
     * @return void
     * @date 2019/9/28 15:33
     * @author 何坤
     */
    @Test
    public void testUpdate(){
        Employee employee = new Employee();
        employee.setId(7);
        employee.setGender(1);
        int i = employeeMapper.updateById(employee);
        logger.info(String.valueOf(i));
    }

    /**
     * 测试基本的查询
     * @param
     * @return void
     * @date 2019/9/28 15:40
     * @author 何坤
     */
    @Test
    public void testSelect01(){
        //方法1：简单的通过主键ID查询
        /*Employee employee = employeeMapper.selectById(4);*/

        //方法2：通过条件的Map集合进行查询
        /*Map<String, Object> map = new HashMap<>();
        map.put("last_name", "yuanbing");
        map.put("age", 12);
        List<Employee> employees = employeeMapper.selectByMap(map);*/

        //方法3：通过queryWrapper
        QueryWrapper<Employee> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("last_name", "yuanbing");
        queryWrapper.eq("age", 12);
        Employee employee = employeeMapper.selectOne(queryWrapper);


        logger.info(employee.toString());
    }

    /**
     * 测试批量查询
     * @param
     * @return void
     * @date 2019/9/28 16:02
     * @author 何坤
     */
    @Test
    public void testSelect02(){
        List<Integer> ids = new ArrayList<>();
        ids.add(1);
        ids.add(3);
        ids.add(5);
        ids.add(7);
        List<Employee> employees = employeeMapper.selectBatchIds(ids);
        logger.info(employees.toString());
    }
    
    /**  
     * 分页查询
     * @param 
     * @return void
     * @date 2019/9/28 16:05
     * @author 何坤
     */       
    @Test
    public void testSelect03(){
        IPage<Employee> page = new Page<>(1, 3);
        QueryWrapper<Employee> queryWrapper = new QueryWrapper<>();
        queryWrapper.between("age", 12, 20);
        IPage<Employee> resultPage = employeeMapper.selectPage(page, queryWrapper);
        logger.info(resultPage.getRecords().toString());
    }

    /**
     * 查询性别为女的，并且根据age进行降序排序
     * @param
     * @return void
     * @date 2019/9/29 13:59
     * @author 何坤
     */
    @Test
    public void testSelect04(){
        QueryWrapper<Employee> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("gender", 0)
                .orderByDesc("age");
        List<Employee> employees = employeeMapper.selectList(queryWrapper);
        logger.info(employees.toString());
    }
}